//
//  MKCartoonViewController.m
//  ManKeCartoon
//
//  Created by dllo on 15/10/19.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKCartoonViewController.h"
#import "MKBaseHeader.h"
#import "StringWithHeight.h"
@interface MKCartoonViewController ()<sendSeq_no, sendPage>

@property (nonatomic,strong)MKReadUpView *readUp;
@property (nonatomic,strong)MKPageView *pageViews;
@property (nonatomic,retain)NSString *str1;
@property (nonatomic,retain)NSString *vip;
@property (nonatomic, retain) MKComicModel *comicModel;

@end

@implementation MKCartoonViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 24, 100, 30)];

    [self.navBarView addSubview:titleLabel];
    
    [titleLabel setText:@"作品详情"];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(10, 24, 30, 30)];
    //[backButton setBackgroundColor:[UIColor redColor]];
    [self.navBarView addSubview:backButton];
    [backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    
#warning downloadButton

    UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    downloadButton.frame = CGRectMake(SCREEN_WIDTH-40, 24, 30, 30);
    [downloadButton setImage:[UIImage imageNamed:@"iconfont-download.png"] forState:UIControlStateNormal];
    [downloadButton addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:downloadButton];
    
    
    // 设置整个页面的底层
    self.ViewScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
    [_ViewScroll setBackgroundColor:[UIColor whiteColor]];
    _ViewScroll.showsVerticalScrollIndicator = NO;
    // 先定义个大的范围,  页面铺完下面再改
    [_ViewScroll setContentSize:CGSizeMake(0, 1200)];
    [self.view addSubview:_ViewScroll];

#pragma mark - 从网络获取数据
    
    self.str1 = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Comic/ComicDetailV2/comic_id/%@/",self.comic_id];
    
    //[AFNETWORK getDataWithNoCacheString:_str1 block:^(id block) {
    
    [AFNETWORK getDataWithString:_str1 block:^(id block) {
        self.TJArray = [[NSMutableArray alloc] init];
        self.mutableDic = [[NSMutableDictionary alloc] init];
        self.chapterModelArray = [[NSMutableArray alloc] init];
        

        // 上半部分数据 是个大字典
        self.mutableDic = [[block objectForKey:@"data"] objectForKey:@"comic"];
        self.vip = [self.mutableDic objectForKey:@"vip_state"];
        //中部区域 page
        for (NSDictionary *dic in [[block objectForKey:@"data"]objectForKey:@"chapters"]) {
            MKChaptersModel *chaptersModel = [[MKChaptersModel alloc] initWithDictionary:dic];
            if ([[NSString stringWithFormat:@"%@", _comic_id] isEqualToString:@"12769"] && chaptersModel.seq_no.integerValue > 58) {
            
            //if (chaptersModel.seq_no.integerValue > 58) {
                chaptersModel.seq_no = [NSString stringWithFormat:@"%ld", chaptersModel.seq_no.integerValue - 1];
            //}
            }
            [self.chapterModelArray addObject:chaptersModel];
            
        }

        _comicModel = [[MKComicModel alloc] initWithDictionary:[[block objectForKey:@"data"] objectForKey:@"comic"]];
        
        //下边推荐 数据
        for (NSDictionary *dic in [[block objectForKey:@"data"]objectForKey:@"recommend"]) {
            
            MKTJModel *model = [[MKTJModel alloc] initWithDictionary:dic];
            [self.TJArray addObject:model];
  
        }

        if (_FMimageView == nil) {
            
            // 中间目录
            [self createUpView];
            
            // 开头上半部分
            [self createType];
            
            // 推荐
            [self createTJview];
            
            // 调用目录 button
            [self createPageViewBtn];
            
            [self createStar];
            
        } else {
        
            [_ViewScroll setNeedsDisplay];
            
        }
        
    }];
    
}
#pragma mark - 下载
- (void)downloadClick
{
    MKDownloadListViewController *download = [[MKDownloadListViewController alloc] init];
    download.comic_id = _comic_id;
//    [self.navigationController pushViewController:download animated:YES];
    if ([_comicModel.vip_state isEqualToString:@"1"]) {
        
        [self.navigationController pushViewController:download animated:YES];
        
    }else{
        
        UIAlertView *AlertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"本漫画是付费漫画暂不提供下载功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [AlertView show];
        
    }
}
#pragma mark - 返回
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];

}
#pragma mark -  评分 星星
-(void)createStar
{
    self.starView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 5, SCREEN_HEIGHT / 3, SCREEN_WIDTH  - SCREEN_WIDTH / 5 * 2, SCREEN_WIDTH / 3)];
    [self.starView setBackgroundColor:[UIColor whiteColor]];
    self.starView.layer.masksToBounds = YES;
    self.starView.layer.cornerRadius = 10.;
    self.starView.layer.borderWidth = 1;
    self.starView.layer.borderColor = [UIColor grayColor].CGColor;
    self.starView.hidden = YES;
    [self.view addSubview:self.starView];
    
    self.sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sureButton setFrame:CGRectMake(0, self.starView.frame.size.height - 40, self.starView.frame.size.width, 40)];
    [self.sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.sureButton.layer.masksToBounds = YES;
    self.sureButton.layer.borderWidth = 1;
    self.sureButton.layer.borderColor = [UIColor grayColor].CGColor;
    self.sureButton.titleLabel.font = [UIFont systemFontOfSize:18];
    self.sureButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.sureButton addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    [self.starView addSubview:self.sureButton];

    self.starArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 5; i++) {
        self.starButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.starButton setFrame:CGRectMake(i * self.starView.frame.size.width / 5, 30, 40, 40)];
        if (SCREEN_HEIGHT != 667) {
            [self.starButton setFrame:CGRectMake(i * self.starView.frame.size.width / 5, 30, 30, 30)];
        }
        [self.starButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"PFXX.png"]] forState:UIControlStateNormal];
        [self.starButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"PFXXX.png"]] forState:UIControlStateSelected];
        [self.starButton addTarget:self action:@selector(starClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.starView addSubview:self.starButton];
        [self.starArray addObject:self.starButton];
    }
    
    UILabel *starLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, self.starView.frame.size.width, 20)];
    [starLabel setText:@"请给本作品评分,谢谢!"];
    [starLabel setTextAlignment:NSTextAlignmentCenter];
    [starLabel setTextColor:[UIColor blackColor]];
    [starLabel setFont:[UIFont systemFontOfSize:15]];
    [self.starView addSubview:starLabel];
    
}

#pragma mark - 引入简介部分控件
-(void)createType
{
    [_FMimageView sd_setImageWithURL:[NSURL URLWithString:[_mutableDic objectForKey:@"cover_url"]]];
    [_Stitle setText:[_mutableDic objectForKey:@"title"]];
    [_Stype setText:[NSString stringWithFormat:@"类型: %@",[_mutableDic objectForKey:@"type"]]];
    [_Sartist_name setText:[NSString stringWithFormat:@"作者: %@",[_mutableDic objectForKey:@"artist_name"]]];
    [_Slastup setText:[NSString stringWithFormat:@"更新至%@",[_mutableDic objectForKey:@"lastup"]]];
    
    if ([[_mutableDic objectForKey:@"vip_state"] isEqualToString:@"2"]) {
        [_TXlabel setHidden:NO];
        [_VIPimage setHidden:NO];
    }else{
        [_TXlabel setHidden:YES];
        [_VIPimage setHidden:YES];
    }
    
    
#pragma mark - 引入简介部分控件
    
    self.readUp = [[MKReadUpView alloc] initWithFrame:CGRectMake(0, _FMimageView.frame.size.height + _FMimageView.frame.origin.y + 10, self.view.frame.size.width, 150)];
    
    if ([[NSUserDefaults standardUserDefaults] valueForKey:_Stitle.text] == nil) {
        
        [self.readUp.SlxLastup setTitle:@"第1话" forState:UIControlStateNormal];
        
    } else {
        
        [self.readUp.SlxLastup setTitle:[[NSUserDefaults standardUserDefaults] valueForKey:_Stitle.text] forState:UIControlStateNormal];
    }

    [_readUp.ScroeButton setTitle:[NSString stringWithFormat:@"评分 %@",[_mutableDic objectForKey:@"grade_ave"]] forState:UIControlStateNormal];
    [_readUp.FXbutton setTitle:@"分享" forState:UIControlStateNormal];
    [_readUp.YPbutton setTitle:[NSString stringWithFormat:@"月票 %@",[_mutableDic objectForKey:@"month_ticket"]] forState:UIControlStateNormal];
    
#pragma mark 设置点击方法  (评分..  分享 ... 月票..
    // 设置点击方法  (评分..  分享 ... 月票.. )
    [_readUp.ScroeButton addTarget:self action:@selector(ScroeClick:) forControlEvents:UIControlEventTouchUpInside];
    [_readUp.FXbutton addTarget:self action:@selector(FXClick:) forControlEvents:UIControlEventTouchUpInside];
    [_readUp.YPbutton addTarget:self action:@selector(YPClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.ViewScroll addSubview:_readUp];
    
    [self createTypeV];
    
    [_brief_intrdLabel setFrame:CGRectMake(10, _readUp.frame.size.height + _readUp.frame.origin.y , _ViewScroll.frame.size.width - 20, 40)];
    [_brief_intrdLabel setText:[NSString stringWithFormat:@"简介:  %@",[_mutableDic objectForKey:@"brief_intrd"]]];
    
    [_ZKbutton setFrame:CGRectMake(self.view.frame.size.width - 60, _brief_intrdLabel.frame.size.height + _brief_intrdLabel.frame.origin.y + 10, 50, 25)];
    [_ZKbutton addTarget:self action:@selector(ZKCLick:) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mark - 创建目录
    
    self.pageViews = [[MKPageView alloc] init];
    
    [_pageViews setFrame:CGRectMake(0, _ZKbutton.frame.size.height + _ZKbutton.frame.origin.y + 5, self.view.frame.size.width, 430)];
    
    [_ViewScroll setContentSize:CGSizeMake(0, _pageViews.frame.size.height + _pageViews.frame.origin.y + 20)];
    
    NSString *str = [[_mutableDic objectForKey:@"update_date"] substringToIndex:10];
    [_pageViews.TimeLabel setText:[NSString stringWithFormat:@"更新时间: %@",str]];
    
#pragma mark 收藏的点击事件
    [self.readUp.ScollectButton addTarget:self action:@selector(shouCangClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.readUp.SlxLastup addTarget:self action:@selector(lastupClick:) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mark 进页面就检测这个漫画有没有被收藏
    NSArray *array = [[DataBase shareDataBase] selectAll];
    for (MKBookModel *model in array) {
        if ([model.comic_id isEqualToString:self.comic_id]) {
            self.readUp.ScollectButton.selected = YES;
            //[self.readUp.SlxLastup setTitle:[NSString stringWithFormat:@"第%@",model.page] forState:UIControlStateNormal];
//            NSLog(@"在收藏的 %@  第%@话 number:%ld",model.comic_id,model.page,model.number);
        }
    }
}


#pragma mark - 为每个button按钮附上对应属性 点击后进入对应的漫画
- (void)createPageViewBtn
{

    if (self.chapterModelArray.count < 8) {
        for (int i = 0; i < self.chapterModelArray.count; i++) {
            
            MKChaptersModel *model = [[MKChaptersModel alloc] init];
//            model = _PageArray[i];
            model = _chapterModelArray[i];
            if (i < 4) {
                _pageViews.pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [_pageViews.pageButton setFrame:CGRectMake(10 + (i * (self.view.frame.size.width - 35) / 4  + 15), _pageViews.TimeLabel.frame.size.height + _pageViews.TimeLabel.frame.origin.y + 15, (self.view.frame.size.width - 35) / 5, 30)];
                [_pageViews.pageButton setTitle:model.seq_no forState:UIControlStateNormal];
                _pageViews.pageButton.layer.masksToBounds = YES;
                [_pageViews.pageButton.layer setCornerRadius:5];
                _pageViews.pageButton.layer.borderWidth = .5;
                _pageViews.pageButton.layer.borderColor = [UIColor grayColor].CGColor;
                [_pageViews.pageButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                _pageViews.pageButton.titleLabel.font = [UIFont systemFontOfSize: 15];
                [_pageViews.pageButton addTarget:self action:@selector(LookClick:) forControlEvents:UIControlEventTouchUpInside];
                [_pageViews addSubview:_pageViews.pageButton];
            }else{
                _pageViews.pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [_pageViews.pageButton setFrame:CGRectMake(10 + ((i - 4) * (self.view.frame.size.width - 35) / 4  + 15), _pageViews.TimeLabel.frame.size.height + _pageViews.TimeLabel.frame.origin.y + 5 + 50, (self.view.frame.size.width - 35) / 5, 30)];
                [_pageViews.pageButton setTitle:model.seq_no forState:UIControlStateNormal];
                _pageViews.pageButton.layer.masksToBounds = YES;
                [_pageViews.pageButton.layer setCornerRadius:5];
                _pageViews.pageButton.layer.borderWidth = .5;
                _pageViews.pageButton.layer.borderColor = [UIColor grayColor].CGColor;
                [_pageViews.pageButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                _pageViews.pageButton.titleLabel.font = [UIFont systemFontOfSize: 15];
                [_pageViews.pageButton addTarget:self action:@selector(LookClick:) forControlEvents:UIControlEventTouchUpInside];
                [_pageViews addSubview:_pageViews.pageButton];
            }

            
        }
        
    } else {
        
        for (int i = 0; i < 8; i++) {
            
            
            MKChaptersModel *model = [[MKChaptersModel alloc] init];
//            model = _PageArray[i];
            model = _chapterModelArray[i];
            if (i < 4) {
                _pageViews.pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [_pageViews.pageButton setFrame:CGRectMake(10 + (i * (self.view.frame.size.width - 35) / 4  + 15), _pageViews.TimeLabel.frame.size.height + _pageViews.TimeLabel.frame.origin.y + 15, (self.view.frame.size.width - 35) / 5, 30)];
                    [_pageViews.pageButton setTitle:model.seq_no forState:UIControlStateNormal];
                _pageViews.pageButton.layer.masksToBounds = YES;
                [_pageViews.pageButton.layer setCornerRadius:5];
                _pageViews.pageButton.layer.borderWidth = .5;
                _pageViews.pageButton.layer.borderColor = [UIColor grayColor].CGColor;
                [_pageViews.pageButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                _pageViews.pageButton.titleLabel.font = [UIFont systemFontOfSize: 15];
                [_pageViews.pageButton addTarget:self action:@selector(LookClick:) forControlEvents:UIControlEventTouchUpInside];
                [_pageViews addSubview:_pageViews.pageButton];
            }else{
                _pageViews.pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [_pageViews.pageButton setFrame:CGRectMake(10 + ((i - 4) * (self.view.frame.size.width - 35) / 4  + 15), _pageViews.TimeLabel.frame.size.height + _pageViews.TimeLabel.frame.origin.y + 5 + 50, (self.view.frame.size.width - 35) / 5, 30)];

                    [_pageViews.pageButton setTitle:model.seq_no forState:UIControlStateNormal];
                _pageViews.pageButton.layer.masksToBounds = YES;
                [_pageViews.pageButton.layer setCornerRadius:5];
                _pageViews.pageButton.layer.borderWidth = .5;
                _pageViews.pageButton.layer.borderColor = [UIColor grayColor].CGColor;
                [_pageViews.pageButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                _pageViews.pageButton.titleLabel.font = [UIFont systemFontOfSize: 15];
                [_pageViews.pageButton addTarget:self action:@selector(LookClick:) forControlEvents:UIControlEventTouchUpInside];
                [_pageViews addSubview:_pageViews.pageButton];
            }

            
        }
    }
    [self.ViewScroll addSubview:_pageViews];
    
    //点击查看全部章节push
    [_pageViews.ALLButton addTarget:self action:@selector(PageClick:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - type简介
-(void)createTypeV
{
    // 简介信息
    self.brief_intrdLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_brief_intrdLabel setNumberOfLines:2];
    [_brief_intrdLabel setTextColor:[UIColor blackColor]];
    [_brief_intrdLabel setFont:[UIFont systemFontOfSize:15]];
    [self.ViewScroll addSubview:_brief_intrdLabel];
    
    
    self.ZKbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_ZKbutton setImageEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    [_ZKbutton setTitleEdgeInsets:UIEdgeInsetsMake(0, -60, 0, 0)];
    _ZKbutton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_ZKbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_ZKbutton setTitleColor:[UIColor colorWithRed:0.90 green:0.2 blue:0.15 alpha:1] forState:UIControlStateSelected];
    [_ZKbutton setTitle:@"展开" forState:UIControlStateNormal];
    [_ZKbutton setTitle:@"收起" forState:UIControlStateSelected];
    [_ZKbutton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"ZKKK.png"]] forState:UIControlStateNormal];
    
    [self.ViewScroll addSubview:_ZKbutton];
    
    
    
}

#pragma mark - 创建作品详情上半部分
-(void)createUpView
{
    // 图片
    _FMimageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH / 3, SCREEN_WIDTH / 2)];
    [self.ViewScroll addSubview:_FMimageView];
    
    //标题
    _Stitle = [[UILabel alloc] initWithFrame:CGRectMake(_FMimageView.frame.size.width + _FMimageView.frame.origin.x + 10, _FMimageView.frame.origin.y + 5, SCREEN_WIDTH - _FMimageView.frame.size.width - _FMimageView.frame.origin.x - 15, _FMimageView.frame.size.height / 8)];
    [_Stitle setTextColor:[UIColor blackColor]];
    [_Stitle setFont:[UIFont systemFontOfSize:18]];
    [self.ViewScroll addSubview:_Stitle];
    
    //信息
    _Stype = [[UILabel alloc] initWithFrame:CGRectMake(_Stitle.frame.origin.x, _Stitle.frame.origin.y + _Stitle.frame.size.height + _Stitle.frame.size.height, _Stitle.frame.size.width, _FMimageView.frame.size.height / 8)];
    [_Stype setTextColor:[UIColor grayColor]];
    [_Stype setFont:[UIFont systemFontOfSize:15]];
    [self.ViewScroll addSubview:_Stype];
    
    _Sartist_name = [[UILabel alloc] initWithFrame:CGRectMake(_Stitle.frame.origin.x, _Stype.frame.size.height + _Stype.frame.origin.y + 10, _Stype.frame.size.width, _Stype.frame.size.height)];
    [_Sartist_name setTextColor:[UIColor grayColor]];
    [_Sartist_name setFont:[UIFont systemFontOfSize:15]];
    [self.ViewScroll addSubview:_Sartist_name];
    
    _Slastup = [[UILabel alloc] initWithFrame:CGRectMake(_Stitle.frame.origin.x, _Sartist_name.frame.size.height + _Sartist_name.frame.origin.y + 10, _Sartist_name.frame.size.width, _Sartist_name.frame.size.height)];
    [_Slastup setTextColor:[UIColor grayColor]];
    [_Slastup setFont:[UIFont systemFontOfSize:15]];
    [self.ViewScroll addSubview:_Slastup];
    
    _VIPimage = [[UIImageView alloc] initWithFrame:CGRectMake(_Stitle.frame.origin.x, _Slastup.frame.size.height + _Slastup.frame.origin.y + 10, 25, 25)];
    [_VIPimage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"VIP.png"]]];
    [self.ViewScroll addSubview:_VIPimage];
    
    _TXlabel = [[UILabel alloc] initWithFrame:CGRectMake(_VIPimage.frame.size.width + _VIPimage.frame.origin.x + 5, _VIPimage.frame.origin.y, _Slastup.frame.size.width - 35, 25)];
    [_TXlabel setTextColor:[UIColor blueColor]];
    [_TXlabel setText:@"此漫画为VIP付费观看作品"];
    [_TXlabel setFont:[UIFont systemFontOfSize:14]];
    [self.ViewScroll addSubview:_TXlabel];
    
}

-(void)createTJview
{
    
#pragma mark 推荐部分~~~~
    for (int i = 0; i < 3; i++) {
        
        MKTJModel *tjModel = [[MKTJModel alloc] init];
        
        if (self.TJArray.count != 0) {
            
        tjModel = self.TJArray[i];
        
        _pageViews.TJimageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 + (i * ((SCREEN_WIDTH - 10) / 3)), _pageViews.TJImage.frame.size.height + _pageViews.TJImage.frame.origin.y + 10, (SCREEN_WIDTH - 30) / 3, (SCREEN_WIDTH - 30) / 3 / 3 * 4 )];
        [_pageViews.TJimageView sd_setImageWithURL:[NSURL URLWithString:tjModel.cover_url]];
        [_pageViews addSubview:_pageViews.TJimageView];
        
        _pageViews.ImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pageViews.ImageButton setFrame:CGRectMake(10 + (i * ((SCREEN_WIDTH - 10) / 3)), _pageViews.TJImage.frame.size.height + _pageViews.TJImage.frame.origin.y + 10, (SCREEN_WIDTH - 30) / 3, (SCREEN_WIDTH - 30) / 3 / 3 * 4 )];
        [_pageViews.ImageButton addTarget:self action:@selector(TJClick:) forControlEvents:UIControlEventTouchUpInside];
        
        // 推荐的三个按钮的tag值
        _pageViews.ImageButton.tag = 10 + i;
        [_pageViews addSubview:_pageViews.ImageButton];
        [_pageViews.ImageButton bringSubviewToFront:_pageViews.TJimageView];
        
        _pageViews.TJnameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 + (i * ((SCREEN_WIDTH - 10) / 3)), _pageViews.TJimageView.frame.size.height + _pageViews.TJimageView.frame.origin.y + 5, (SCREEN_WIDTH - 30) / 3, 20)];
        [_pageViews.TJnameLabel setText:tjModel.title];
        [_pageViews.TJnameLabel setFont:[UIFont systemFontOfSize:14]];
        [_pageViews.TJnameLabel setTextColor:[UIColor grayColor]];
        [_pageViews addSubview:_pageViews.TJnameLabel];

        }
    }
}

#pragma mark - 在navigation上铺个返回键
-(void)BackClick
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 展开简介的方法
-(void)ZKCLick:(UIButton *)sender
{
    
    sender.selected = !sender.selected;
    
    _TextHeight =  [StringWithHeight HeightWithText:[_mutableDic objectForKey:@"brief_intrd"] width:self.view.frame.size.width - 20. font:[UIFont systemFontOfSize:15.]];
    if (sender.selected == YES) {
        [_brief_intrdLabel setNumberOfLines:0];
        [_brief_intrdLabel setFrame:CGRectMake(10, _readUp.frame.size.height + _readUp.frame.origin.y , _ViewScroll.frame.size.width - 20, _TextHeight)];
    } else {
        [_brief_intrdLabel setNumberOfLines:2];
        [_brief_intrdLabel setFrame:CGRectMake(10, _readUp.frame.size.height + _readUp.frame.origin.y , _ViewScroll.frame.size.width - 20, 40)];
    }
    [_ZKbutton setFrame:CGRectMake(self.view.frame.size.width - 60, _brief_intrdLabel.frame.size.height + _brief_intrdLabel.frame.origin.y + 10, 50, 25)];
    [_pageViews setFrame:CGRectMake(0, _ZKbutton.frame.size.height + _ZKbutton.frame.origin.y + 5, self.view.frame.size.width, 430)];
    [_ViewScroll setContentSize:CGSizeMake(0, _pageViews.frame.size.height + _pageViews.frame.origin.y + 20)];
    
}

#pragma mark 点击查看全部章节
-(void)PageClick:(UIButton *)sender
{
    
    MKPageViewController *page = [[MKPageViewController alloc] init];
    page.pageNumber = self.readUp.SlxLastup.titleLabel.text;
    
    page.vip_state = self.vip;
    //  把需要的数据传过去
    page.sendPageDelegate = self;
    page.str1 = _str1;
    if (_chapterModelArray.count > 8) {
        
        [self.navigationController pushViewController:page animated:NO];
    }

}
- (void)sendPage:(NSString *)string{
    
    [self.readUp.SlxLastup setTitle:string forState:UIControlStateNormal];
    [[NSUserDefaults standardUserDefaults] setValue:string forKey:_Stitle.text];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)sendSeq_no:(NSString *)string{
    
    [self.readUp.SlxLastup setTitle:string forState:UIControlStateNormal];
    [[NSUserDefaults standardUserDefaults] setValue:string forKey:_Stitle.text];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

#pragma mark - 点击目录进入对应漫画
-(void)LookClick:(UIButton *)sender
{
    MKBrowseCartoonViewController *browse = [[MKBrowseCartoonViewController alloc] init];

    //NSString *string = [NSString stringWithFormat:@"第%@话", sender.titleLabel.text];
    browse.delegate = self;
    for (MKChaptersModel *model in _chapterModelArray ) {
        if ([model.seq_no isEqualToString:sender.titleLabel.text]) {
            browse.chapterModel = model;
            browse.seq_no = model.seq_no.integerValue;
            browse.comicModel = _comicModel;
            browse.chapterModelArray = _chapterModelArray;
        }
    }

    if ([_comicModel.vip_state isEqualToString:@"1"]) {
        
        [self.navigationController pushViewController:browse animated:NO];
        
    }else{
        
        UIAlertView *AlertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"本漫画是付费漫画下次版本更新会提供观看" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [AlertView show];

    }

}




#pragma mark - 设置点击方法  (评分..  分享 ... 月票..

//评分
-(void)ScroeClick:(UIButton *)sender
{
    
    NSLog(@"评分");
    self.starView.hidden = NO;
}

//分享
-(void)FXClick:(UIButton *)sender
{
//    NSString *str = @"分享";
//    NSLog(@"%@",str);
//    [MobClick event:@"FXClick"];
//#pragma mark UM
//    [UMSocialSnsService presentSnsIconSheetView:self
//                                         appKey:@"507fcab25270157b37000010"
//                                      shareText:@"Can漫画,新的体验"
//                                     shareImage:[UIImage imageNamed:@"icon.png"]
//                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
//                                       delegate:self];
//    
//    [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline]];
    
    
}

// 月票
-(void)YPClick:(UIButton *)sender
{
    
    UIAlertView *monthPear = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"下个版本会推出手机客户端送月票功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [monthPear show];
    NSLog(@"月票");

    
    
    
}

#pragma mark - 推荐的点击按钮
-(void)TJClick:(UIButton *)sender
{
    
    NSLog(@"推荐");
    
    
    //     获取对应数据push到新的漫画
    
    if (sender.tag == 10) {
        MKCartoonViewController *cartoon = [[MKCartoonViewController alloc] init];
        MKTJModel *model = [[MKTJModel alloc] init];
        model = _TJArray[0];
        cartoon.comic_id = model.comic_id;
        [self.navigationController pushViewController:cartoon animated:NO];

    }else if (sender.tag == 11){
        MKCartoonViewController *cartoon = [[MKCartoonViewController alloc] init];
        MKTJModel *model = [[MKTJModel alloc] init];
        model = _TJArray[1];
        cartoon.comic_id = model.comic_id;
        [self.navigationController pushViewController:cartoon animated:NO];
    }else if (sender.tag == 12){
        MKCartoonViewController *cartoon = [[MKCartoonViewController alloc] init];
        MKTJModel *model = [[MKTJModel alloc] init];
        model = _TJArray[2];
        cartoon.comic_id = model.comic_id;
        [self.navigationController pushViewController:cartoon animated:NO];
    }
    
}

#pragma mark - 收藏按钮还有页码记录的按钮
-(void)shouCangClick:(UIButton *)sender
{
    
    //NSLog(@"漫画comic_id %@",self.comic_id);
    MKBookModel *bbokmodel = [[MKBookModel alloc] init];
    bbokmodel.comic_id = self.comic_id;
    NSString *str = [self.readUp.SlxLastup.titleLabel.text substringFromIndex:1];
    bbokmodel.page = str;
    bbokmodel.cover_url = [self.mutableDic objectForKey:@"cover_url"];
    bbokmodel.lastup = [self.mutableDic objectForKey:@"lastup"];
    bbokmodel.title = [self.mutableDic objectForKey:@"title"];
    
    NSArray *array = [[NSArray alloc] init];
    array = [[DataBase shareDataBase] selectAll];
    
    //判断button点击状态加入或者删除
    if (sender.selected == NO) {
        [[DataBase shareDataBase] insertBook:bbokmodel];
        NSLog(@"添加 %@  %@  %@   %@  number:%ld",bbokmodel.comic_id,bbokmodel.page,bbokmodel.cover_url,bbokmodel.lastup,bbokmodel.number);
    }else if(sender.selected == YES){
        for (MKBookModel *model in array) {
            if ([bbokmodel.comic_id isEqualToString:model.comic_id]) {
                [[DataBase shareDataBase] deleteBook:model.title];
            }
        }
    }
    
    sender.selected = !sender.selected;
    
}
-(void)lastupClick:(UIButton *)sender
{
    
    MKBrowseCartoonViewController *browse = [[MKBrowseCartoonViewController alloc] init];
    browse.delegate = self;
    NSString *string = [sender.titleLabel.text stringByReplacingOccurrencesOfString:@"第" withString:@""];
    NSString *newString = [string stringByReplacingOccurrencesOfString:@"话" withString:@""];
    
    for (MKChaptersModel *model in _chapterModelArray ) {
        if ([model.seq_no isEqualToString:newString]) {
            browse.chapterModel = model;
            browse.seq_no = model.seq_no.integerValue;
            browse.comicModel = _comicModel;
            browse.chapterModelArray = _chapterModelArray;
        }
    }

    
    NSString *str;
    if (sender.titleLabel.text.length == 3) {
        str = [sender.titleLabel.text substringWithRange:NSMakeRange(1, 1)];
    } else if(sender.titleLabel.text.length == 4){
        str = [sender.titleLabel.text substringWithRange:NSMakeRange(1, 2)];
    }else if(sender.titleLabel.text.length == 5){
        str = [sender.titleLabel.text substringWithRange:NSMakeRange(1, 3)];
    }
    //NSLog(@"那夜 %@",str);

    if (_TXlabel.hidden == YES) {
        
        [self.navigationController pushViewController:browse animated:NO];
    }else{
        UIAlertView *AlertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"本漫画是付费漫画下次版本更新会提供观看" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [AlertView show];

    }
    
}

#pragma mark - 星星 评分
-(void)starClick:(UIButton *)sender
{
    int star = 0;
    for (int i = 0; i < 5; i++) {
        if (sender == self.starArray[i]) {
            star = i;
        }
    }
    for (int i = 0; i <= star; i++) {
        UIButton *starbu = self.starArray[i];
        starbu.selected = YES;
    }
    for (int i = star + 1; i < 5; i++) {
        UIButton *starbu = self.starArray[i];
        starbu.selected = NO;
    }
}
-(void)sureClick
{
    self.starView.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
